package com.pty.test.component;

import java.util.Map;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import com.pty.test.util.TestAnnotation;

/**
 * 基于{@link RequestMapping}的 {@link TestAnnotation}注解解释器
 * 
 * @author Empty
 *
 */
@Component
public class CustRequestMappingHandlerMapping extends RequestMappingHandlerMapping implements InitializingBean {

	/**
	 * 初始化标识符
	 */
	private boolean initFlag = false;

	@Override
	public void afterPropertiesSet() {
		// TODO Auto-generated method stub
		super.afterPropertiesSet();
		if (!initFlag) {
			Map<RequestMappingInfo, HandlerMethod> map = getHandlerMethods();

			map.entrySet().stream().forEach(e -> {
				RequestMappingInfo requestMappingInfo = e.getKey();
				HandlerMethod method = e.getValue();
				TestAnnotation annotation = method.getMethodAnnotation(TestAnnotation.class);
				if (annotation != null) {
					System.out.println("requestMappingInfo " + requestMappingInfo.getPatternsCondition());
					System.out.println("annotation " + annotation.value());
				} else {
					System.out.println("requestMappingInfo " + requestMappingInfo.getName());
					System.out.println("annotation -> no annotation of TestAnnotation");
				}
			});
			initFlag = true;
		}

	}

}
